<?php

namespace common\models\book;

use Yii;

/**
 * This is the model class for table "{{%stage}}".
 *
 * @property int $id
 * @property string $name 时段
 * @property int $start_hour 时段开始时间
 * @property int $end_hour 时段截止时间
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Stage extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    public static $date;
    public static $bookId;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%stage}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['start_hour', 'end_hour', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '时段',
            'start_hour' => '时段开始时间',
            'end_hour' => '时段截止时间',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getDateStage() {
        return $this->hasOne(StageDate::class, ['date' => '']);
    }

    public function getStartHour() {
        return gmdate('H:i', $this->start_hour);
    }

    public function getEndHour() {
        return gmdate('H:i', $this->end_hour);
    }

    /**
     * {@inheritdoc}
     */
    public function getDisabled() {
        $date = static::$date ?: date('Y-m-d', strtotime('+2 days'));
        $disableDates = StageDate::findDisableDate($date, $this->id);
        if ($disableDates) {
            return true;
        }
        $query = RoomStage::find()->alias('t')->andWhere([
            't.date' => $date,
            't.start_hour' => $this->start_hour,
            't.end_hour' => $this->end_hour,
        ]);
        $query->innerJoinWith('book book');
        $query->andWhere([
            'and',
            ['book.deleted_at' => 0],
            ['>=', 'book.status', Book::STATUS_ACTIVE],
            ['<', 'book.status', Book::STATUS_COMPLETE],
        ]);
        if (static::$bookId) {
            $query->andWhere([
                '!=', 'book.id', static::$bookId
            ]);
        }
        $query->debug();
        return $query->exists();
    }

    /**
     * {@inheritdoc}
     */
    public function fieldConfigs() {
        return [
            'id',
            'name',
            'start_hour',
            'end_hour',
            'disabled',
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findAllEffective($date = null, $bookId = null) {
        static::$date = $date;
        static::$bookId = $bookId;
        $query = static::find()->active(static::STATUS_ACTIVE);
        return $query->all();
    }

}
